﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QueueUnlimited.API;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using System.Web.UI;

namespace MultiplayerDice.Implementation
{
    public static class HelperMethods
    {
        public static T IsOfTypeOrNull<T>(this GenericEvent @event) where T : class
        {
            if (@event.Name == typeof(T).FullName)
                return @event.Deserialize<T>();
            return default(T);
        }

        public static T Deserialize<T>(this GenericEvent @event) where T : class
        {
            var serializer = new LosFormatter();
            using (var stream = new MemoryStream())
            using (var streamWriter = new StreamWriter(stream))
            {
                streamWriter.Write(@event.Content);
                streamWriter.Flush();
                stream.Position = 0;
                return serializer.Deserialize(stream) as T;
            }
        }
    }
}
